﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Ext.Net;

public partial class SYS_POPEDOM_Dept_Manage : BasePage
{

    private static string clickNodeId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BuildTreeRoot();
            AddPopWin();
        }             
    }

    //添加弹出窗口
    private void AddPopWin()
    {
        string url = "";
        url = G_Url_allstring + "/SYS_POPEDOM/Dept_Create.aspx";
        extBtn_Add.Listeners.Click.Handler = "popforwin(GridPanel1,'" + url + "','部门信息',500,300,'ObjId','Add',0,'type=0&Parent_ID=' + #{TreePanel1}.getSelectionModel().getSelectedNode().id + '&PARENT_NAME=' + #{TreePanel1}.getSelectionModel().getSelectedNode().text,1);";
        extBtn_Edit.Listeners.Click.Handler = "popforwin(GridPanel1,'" + url + "','部门信息',500,300,'DeptID','DEPT_ID',0,'type=0',2);";
        GridPanel1.Listeners.RowDblClick.Handler = "popforwin(GridPanel1,'" + url + "','部门信息',500,300,'DeptID','DEPT_ID',rowIndex,'type=0',3);";
        
    }

    //构建部门树根节点
    private void BuildTreeRoot()
    {
        DataTable tmpDt = CommonLib.BLL.SYS_DEPT.GetListSub(" dept_id='00' and  del_flag =0 ", "dept_no").Tables[0];
        if (tmpDt.Rows.Count == 1)
        {
            Ext.Net.AsyncTreeNode root = new Ext.Net.AsyncTreeNode();
            root.Text = tmpDt.Rows[0]["dept_name"].ToString();
            root.NodeID = tmpDt.Rows[0]["dept_id"].ToString();
            root.Expanded = true;
            root.IconCls = "no-icon";
            if (tmpDt.Rows[0]["leaf"].ToString() == "0")
                root.Leaf = true;
            this.TreePanel1.Root.Add(root);
        }
    }

    //加载下属节点
    [DirectMethod]
    public string NodeLoad(string nodeID)
    {
        Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
        if (!string.IsNullOrEmpty(nodeID))
        {
            DataTable TreeData = CommonLib.BLL.SYS_DEPT.GetListSub(" del_flag =0 and rtrim(ltrim(dept_id)) like '" + nodeID.Trim() + "__' ", "dept_no").Tables[0];
            for (int i = 0; i < TreeData.Rows.Count; i++)
            {
                AsyncTreeNode asyncNode = new AsyncTreeNode();
                asyncNode.Text = TreeData.Rows[i]["dept_name"].ToString();
                asyncNode.NodeID = TreeData.Rows[i]["dept_id"].ToString();
                asyncNode.IconCls = "no-icon";
                if (TreeData.Rows[i]["leaf"].ToString() == "0")
                    asyncNode.Leaf = true;
                nodes.Add(asyncNode);
            }
        }

        clickNodeId = "";
        BindGrid(nodeID, "NodeClick");
        return nodes.ToJson();
    }

    protected void extBtn_Find_Click(object sender, DirectEventArgs e)
    {
        BindGrid(clickNodeId, "Find_Click");
    }

    //单击树节点


    

    [DirectMethod]
    public void NodeClick(string NodeId)
    {
        clickNodeId = NodeId;
        BindGrid(NodeId, "NodeClick");
    }

    //加载列表
    private void BindGrid(string strParentId,string searchType)
    {
        string sqlw = string.Format(" is_supply=0 and del_flag =0 and rtrim(ltrim(PARENT_ID))='{0}'  ", strParentId.Trim());
        DataTable GridData = null;
        if (searchType == "Find_Click")
        {

            string sql = string.Format(@"with cte as
(
    select * from( select a.*,b.DEPT_NAME PARENT_NAME from SYS_DEPT a LEFT join SYS_DEPT b on b.DEPT_ID = a.PARENT_ID ) t
    where {0}
    union all
    select d.*,c.DEPT_NAME PARENT_NAME from cte c inner join SYS_DEPT d
    on c.DEPT_ID = d.PARENT_ID
)
select * from cte
where ( DEPT_NO like '%{1}%' OR DEPT_NAME LIKE '%{1}%' OR DEPT_PYM LIKE '%{1}%' )
order by dept_no
", sqlw, exttxtf_dept_name.Text.Trim());

            GridData = CommonLib.BLL.BS_DATASET_SQL.SqlGetTable(sql);
        }
        else
        {
            GridData = CommonLib.BLL.SYS_DEPT.GetListSub(sqlw, "dept_no").Tables[0];
        }

        this.Store1.DataSource = GridData;
        this.Store1.DataBind();
        //SelectFristData(GridPanel1, GridData, "");
    }

    //删除部门
    protected void DelDept(object sender, DirectEventArgs e)
    {
        int nCount = 0;
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        nCount = sm.SelectedRows.Count;
        if (nCount == 0)
        {
            X.Msg.Alert("系统提示", "请先选择要删除的记录！").Show();
            return;
        }

        X.Msg.Confirm("系统提示", "是否删除选中行?", new MessageBoxButtonsConfig
        {
            Yes = new MessageBoxButtonConfig
            {
                Handler = "Ext.net.DirectMethods.DoDelMenu();refreshTree();",
                Text = "是"
            },
            No = new MessageBoxButtonConfig
            {
                Text = "否"
            }
        }).Show();

    }

    //删除菜单
    [DirectMethod]
    public void DoDelMenu()
    {
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        foreach (SelectedRow row in sm.SelectedRows)
        {

            if (CommonLib.BLL.SYS_DEPT.GetRecorderCount(" DEL_FLAG=0 and ltrim(rtrim(parent_id)) ='" + row.RecordID.Trim() + "'") > 0)
            {
                X.Msg.Alert("系统提示", "节点被引用不能删除！").Show();
                continue;
            }
            if (CommonLib.BLL.SYS_USER.GetRecorderCount(" DEL_FLAG=0 and ltrim(rtrim(DEPT_ID)) ='" + row.RecordID.Trim() + "'") > 0)
            {
                X.Msg.Alert("系统提示", "该部门下有员工，不能删除！").Show();
                continue;
            }
            CommonLib.Model.SYS_DEPT oDept = CommonLib.BLL.SYS_DEPT.GetModel(row.RecordID.Trim());
            oDept.DEL_FLAG = 1;
            CommonLib.BLL.SYS_DEPT.Update(oDept);
        }

    }

}
